//---------------------------------------------------------------------------

#include "vcl_stl.h"
#pragma hdrstop

#include "h_base.h"
#include "h_xml.h"
#include "h_global.h"
using namespace h_base;
using namespace h_xml;

#include "h_g.h"
#include "h_i18n.h"
#include "h_stage.h"
#include "t_dummyform.h"

#include "t_eventfireform.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TEventFireForm *EventFireForm;
//---------------------------------------------------------------------------
__fastcall TEventFireForm::TEventFireForm(TComponent* Owner)
   : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TEventFireForm::ButtonOkClick(TObject *Sender)
{
   UTF8String ErrorString;

   Info.Name = EditName->Text.UpperCase().TrimRight();
   Info.HostName = EditHostName->Text.TrimRight();
   Info.Event = EditEvent->Text.UpperCase().TrimRight();
   Info.Description = GetStringFromMemo(MemoDescription);
   Info.SpecialDate = ComboBoxSpecialDate->Text.TrimRight();
   Info.CondNot = CheckBoxCondNot->Checked;
   Info.ConditionFile = EditConditionFile->Text.TrimRight();
   Info.JobTrigger = EditJobTrigger->Text.TrimRight();
   Info.Priority = EditPriority->Text.ToIntDef(0);
   Info.AutoRerun = EditAutoRerun->Text.TrimRight();
   Info.Tag = EditTag->Text.TrimRight();

   Info.ExternJobList.clear();

   for ( set<UTF8String>::iterator Iter = ExternJobSet.begin();
         Iter != ExternJobSet.end(); Iter++ )
   {
      Info.ExternJobList.push_back(Iter->c_str());
   }

   if( !FStage->SetInfo(Info, ErrorString) )
   {
      ShowMessage(ErrorString);
      return;
   }

   OkFlag = true;
   Close();
}
//---------------------------------------------------------------------------

void __fastcall TEventFireForm::ButtonCancelClick(TObject *Sender)
{
   Close();
}
//---------------------------------------------------------------------------

bool __fastcall TEventFireForm::Edit(HStageEventFire *Stage)
{
   FStage = Stage;
   FStage->GetInfo(Info);
   UTF8String Cap;
   Cap.sprintf(UTF8String(DummyForm->PropertiesOf->Caption).c_str(), "event fire");
   Caption = Cap;
   EditName->Text = Info.Name;
   EditHostName->Text = Info.HostName;
   EditEvent->Text = Info.Event;
   SetStringToMemo(MemoDescription, Info.Description);
   CreateSpecialDateList(ComboBoxSpecialDate, h_g::SpecialDateList);
   ComboBoxSpecialDate->Text = Info.SpecialDate;
   CheckBoxCondNot->Checked = Info.CondNot;
   EditConditionFile->Text = Info.ConditionFile;
   EditJobTrigger->Text = Info.JobTrigger;
   EditPriority->Text = Info.Priority;
   EditAutoRerun->Text = Info.AutoRerun;
   EditTag->Text = Info.Tag;

   ExternJobSet.clear();

   for ( int i = 0; i < Info.ExternJobList.size(); i++ )
   {
      ExternJobSet.insert(Info.ExternJobList[i].c_str());
   }

   GridExternJobAssign();
   OkFlag = false;

   ShowModal();

   if( !OkFlag )
      return false;

   return true;
}



void __fastcall TEventFireForm::ButtonAdd2Click(TObject *Sender)
{
   UTF8String NewGroup = EditExternGroup->Text.UpperCase().TrimRight();
   UTF8String NewJob = EditExternJob->Text.UpperCase().TrimRight();

   if ( NewGroup != "" )
   {
      if ( NewJob == "" )
      {
         ExternJobSet.insert(NewGroup);
      }
      else
      {
         ExternJobSet.insert(NewGroup + ":" + NewJob);
      }
      GridExternJobAssign();
   }
}
//---------------------------------------------------------------------------

void __fastcall TEventFireForm::ButtonRemove2Click(TObject *Sender)
{
   int Top = GridExternJob->Selection.Top;
   int Bottom = GridExternJob->Selection.Bottom;

   if ( Top > Bottom )
   {
      return;
   }

   UTF8String Key;
   int i = 1;

   for ( set<UTF8String>::const_iterator Iter = ExternJobSet.begin();
         Iter != ExternJobSet.end(); Iter++, i++ )
   {
      if ( i == Top )
      {
         Key = *Iter;
         break;
      }
   }

   if ( Key != "" )
   {
      ExternJobSet.erase(Key);
      GridExternJobAssign();
   }
}
//---------------------------------------------------------------------------

void __fastcall TEventFireForm::ButtonClear2Click(TObject *Sender)
{
   ExternJobSet.clear();
   GridExternJobAssign();
}
//---------------------------------------------------------------------------
void __fastcall TEventFireForm::GridExternJobAssign()
{
   TStringGrid *Grid = GridExternJob;

   Grid->ColWidths[0] = ( Grid->Width - 32 ) * 1 / 4;
   Grid->ColWidths[1] = ( Grid->Width - 32 ) * 3 / 4;

   if ( ExternJobSet.empty() )
   {
      Grid->RowCount = 2;
      Grid->Cells[0][1] = "";
      Grid->Cells[1][1] = "";
   }
   else
   {
      Grid->RowCount = ExternJobSet.size() + 1;
   }

   Grid->Cells[0][0] = UTF8String(h_g::I18nString("t_normaljobform:EXTERN_GROUP"));
   Grid->Cells[1][0] = UTF8String(h_g::I18nString("t_normaljobform:EXTERN_JOB"));

   int i = 0;

   for ( set<UTF8String>::const_iterator Iter = ExternJobSet.begin();
         Iter != ExternJobSet.end(); Iter++, i++ )
   {
      char w[200 + 1];
      int Pos = 0;
      Pos += ReadWord(Iter->c_str() + Pos, w, ": ", 200);
      Grid->Cells[0][i + 1] = w;
      Pos += ReadWord(Iter->c_str() + Pos, w, ": ", 200);
      Pos += ReadWord(Iter->c_str() + Pos, w, ": ", 200);
      Grid->Cells[1][i + 1] = w;
   }
}

